﻿using System;
using System.Runtime.InteropServices;

namespace UtilZ.Dotnet.Ex.Model
{
    /// <summary>
    /// 指针释放器
    /// </summary>
    unsafe public struct PtrDisposabler : IDisposable
    {
        private void* _ptr;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ptr">要释放的指针</param>
        public PtrDisposabler(void* ptr)
        {
            this._ptr = ptr;
        }

        /// <summary>
        /// 释放指针
        /// </summary>
        public void Dispose()
        {
            if (this._ptr != null)
            {
                NativeMemory.Free(this._ptr);
                //Marshal.FreeHGlobal((IntPtr)this._ptr);
                this._ptr = null;
            }
        }
    }
}
